package com.sktlab.bizconfmobile.net;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.sktlab.bizconfmobile.activity.AppClass;
import com.sktlab.bizconfmobile.model.ConferenceAccount;
import com.sktlab.bizconfmobile.model.Constant;
import com.sktlab.bizconfmobile.model.manager.AccountsManager;
import com.sktlab.bizconfmobile.util.MD5Util;
import com.sktlab.bizconfmobile.util.Util;

public class NetOp {
	
	public static final String TAG = "Net";
	
	public static void downloadAccount(){
		
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		
		String phoneNumber = Util.getSPStringValue(AppClass.getInstance(),
				Constant.KEY_SP_VERIFIED_PHONE_NUM, "");
		
		/******************TEST CODE***********************/
		//phoneNumber = "13910160053";
		/******************TEST CODE***********************/
		
		params.add(new BasicNameValuePair("type", "teList"));
		params.add(new BasicNameValuePair("phoneNumber", phoneNumber));
		params.add(new BasicNameValuePair("ciphertext", MD5Util
				.Md5(phoneNumber + "bizconf")));
		
		HttpTools httpTools = new HttpTools(AppClass.getInstance());

		String rsp = httpTools.doPost(Constant.URL_VERIFY_PHONE_NUMBER,
				params);

		//Util.BIZ_CONF_DEBUG(TAG, "rsp: " + rsp);
		
		String statusCode = "";
		
		try {

			JSONObject jsonObj = new JSONObject(rsp);
			
			statusCode = jsonObj.getString("code");
			JSONArray accountList = jsonObj.getJSONArray("list");
			
			int count = accountList.length();
			
			for(int i = 0; i < count; i++) {
				
				JSONObject obj = (JSONObject)accountList.get(i);
				
				String AccessNumber = obj.getString("accessnumber");
				String confCode = obj.getString("meetingnumber");
				String moderatorPw = obj.getString("hostpassword");
				
				ConferenceAccount account = new ConferenceAccount();
				account.setDialInNumber(AccessNumber);
				account.setConfCode(confCode);
				account.setModeratorPw(moderatorPw);
				
				AccountsManager.getInstance().addAccount(account);
			}
			
			//Util.BIZ_CONF_DEBUG(TAG, "check phone number code " + statusCode);
		} catch (JSONException e) {

			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * @param verifyPhoneNum the number had been verified
	 * @param inputCode the check code user input
	 * @return status code 100 is success, others see the api doc
	 */
    public static String checkInputCode(String verifyPhoneNum, String inputCode) {
    	
    	List<NameValuePair> params = new ArrayList<NameValuePair>();

		params.add(new BasicNameValuePair("type", "telCheckCode"));
		params.add(new BasicNameValuePair("phoneNumber", verifyPhoneNum));
		params.add(new BasicNameValuePair("ciphertext", MD5Util
				.Md5(verifyPhoneNum + "bizconf")));
		params.add(new BasicNameValuePair("verifyCode", inputCode));
		HttpTools httpTools = new HttpTools(AppClass.getInstance());

		String rsp = httpTools.doPost(Constant.URL_VERIFY_PHONE_NUMBER,
				params);

		Util.BIZ_CONF_DEBUG(TAG, "rsp: " + rsp);
		
		String statusCode = "";
		
		try {

			JSONObject jsonObj = new JSONObject(rsp);

			statusCode = jsonObj.getString("code");

			//Util.BIZ_CONF_DEBUG(TAG, "check phone number code " + statusCode);
		} catch (JSONException e) {

			e.printStackTrace();
		}
		
		return statusCode;
    }
    
    /**
     * 
     * @param phoneNumber the number to verify
     * @return status code 100 is success, others see the api doc
     */
    public static String sendNumberToVerify(String phoneNumber) {
    	
    	List<NameValuePair> params = new ArrayList<NameValuePair>();

		params.add(new BasicNameValuePair("type", "telVerifyCode"));
		params.add(new BasicNameValuePair("phoneNumber", phoneNumber));
		params.add(new BasicNameValuePair("ciphertext", MD5Util
				.Md5(phoneNumber + "bizconf")));

		HttpTools httpTools = new HttpTools(AppClass.getInstance());

		String rsp = httpTools.doPost(Constant.URL_VERIFY_PHONE_NUMBER,
				params);

		//Util.BIZ_CONF_DEBUG(TAG, "rsp: " + rsp);
		
		String statusCode = "";
		
		try {

			JSONObject jsonObj = new JSONObject(rsp);
			statusCode = jsonObj.getString("code");

			//Util.BIZ_CONF_DEBUG(TAG, "status code: " + statusCode);
		} catch (JSONException e) {

			e.printStackTrace();
		}
		
		return statusCode;
    }  
}
